package com.chillis.standings.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.chillis.standings.R;
import com.chillis.standings.dispatch.TableManager;
import com.chillis.standings.domain.exceptions.FormatNotFoundException;
import com.chillis.standings.domain.table.TableSettings;
import com.chillis.standings.ui.table.TableActivity;
import com.michaelnovakjr.numberpicker.NumberPickerDialog;

public class SettingsActivity extends Activity implements View.OnClickListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.view_settings);

      configureView();
   }

   private void configureView() {
      View buttonFormat = findViewById(R.id.button_format);
      buttonFormat.setOnClickListener(this);
      ((TextView)buttonFormat.findViewById(R.id.title)).setText(R.string.button_format);
      ((TextView)buttonFormat.findViewById(R.id.review)).setText(checkedFormat());

      View buttonParticipantsCount = findViewById(R.id.button_participants_count);
      buttonParticipantsCount.setOnClickListener(this);
      ((TextView)buttonParticipantsCount.findViewById(R.id.title)).setText(R.string.button_participants_count);
      ((TextView)buttonParticipantsCount.findViewById(R.id.review)).setText(String.valueOf(checkedParticipantsCount()));

      View buttonParticipants = findViewById(R.id.button_participants);
      buttonParticipants.setOnClickListener(this);
      ((TextView)buttonParticipants.findViewById(R.id.title)).setText(R.string.button_participants);

      findViewById(R.id.button_create).setOnClickListener(this);
   }

   private String checkedFormat() {
      TableSettings.Format format = TableSettings.Format.byIndex(this, _checkedFormatIndex);

      return format.toString(this);
   }

   @Override
   public void onClick(View view) {
      if (view.getId() == R.id.button_format) {
         onFormatClicked();
      } else if (view.getId() == R.id.button_participants_count) {
         onParticipantsCountClicked();
      } else if (view.getId() == R.id.button_create) {
         onCreateClicked();
      } else if (view.getId() == R.id.button_participants) {
         onParticipantsClicked();
      }
   }

   private void onParticipantsClicked() {
      createTable();

      startActivity(new Intent(this, ParticipantsActivity.class));
   }

   private void onCreateClicked() {
      createTable();

      startActivity(new Intent(this, TableActivity.class));
   }

   private void createTable() {
      TableSettings.Format format = TableSettings.Format.byIndex(this, _checkedFormatIndex);

      TableManager.instance().updateSettings(format, checkedParticipantsCount());
   }

   private void onFormatClicked() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(R.string.button_format)
             .setSingleChoiceItems(TableSettings.availableFormats(this), _checkedFormatIndex, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                   checkedFormatChanged(which);

                   dialog.dismiss();
                }
             })
             .create()
             .show();
   }

   private void checkedFormatChanged(int which) {
      _checkedFormatIndex = which;

      updateCheckedFormat();
      updateCheckedParticipantsCount();
   }

   private void updateCheckedFormat() {
      View buttonFormat = findViewById(R.id.button_format);
      ((TextView)buttonFormat.findViewById(R.id.review)).setText(checkedFormat());
   }

   private void onParticipantsCountClicked() {
      TableSettings.Format checkedFormat = TableSettings.Format.byIndex(this, _checkedFormatIndex);

      if (checkedFormat == TableSettings.Format.Round) {
         showParticipantsCountNumberPickerDialog();
      } else if (checkedFormat == TableSettings.Format.Olympic) {
         showParticipantsCountListDialog();
      }
   }

   private void showParticipantsCountListDialog() {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setTitle(R.string.button_participants_count)
              .setSingleChoiceItems(TableSettings.availableParticipantsCountsForRoundFormat(), _checkedParticipantsCountIndex, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int which) {
                    checkedParticipantsCountChanged(which);

                    dialog.dismiss();
                 }
              })
              .create()
              .show();
   }

   private void showParticipantsCountNumberPickerDialog() {
      final NumberPickerDialog dialog = new NumberPickerDialog(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, _checkedParticipantsCount);

      dialog.setTitle(R.string.button_participants_count);
      dialog.setOnNumberSetListener(new NumberPickerDialog.OnNumberSetListener() {
         @Override
         public void onNumberSet(int selectedNumber) {
            _checkedParticipantsCount = selectedNumber;
            updateCheckedParticipantsCount();

            dialog.dismiss();
         }
      });

      dialog.show();
   }

   private void updateCheckedParticipantsCount() {
      View buttonParticipantsCount = findViewById(R.id.button_participants_count);
      ((TextView)buttonParticipantsCount.findViewById(R.id.review)).setText(String.valueOf(checkedParticipantsCount()));
   }

   private void checkedParticipantsCountChanged(int which) {
      _checkedParticipantsCountIndex = which;

      updateCheckedParticipantsCount();
   }

   private int checkedParticipantsCount() {
      TableSettings.Format checkedFormat = TableSettings.Format.byIndex(this, _checkedFormatIndex);
      int checkedParticipantsCount;

      if (checkedFormat == TableSettings.Format.Round) {
         checkedParticipantsCount = _checkedParticipantsCount;
      } else if (checkedFormat == TableSettings.Format.Olympic) {
         checkedParticipantsCount = Integer.parseInt(TableSettings.availableParticipantsCountsForRoundFormat()[_checkedParticipantsCountIndex]);
      } else {
         throw new FormatNotFoundException();
      }

      return checkedParticipantsCount;
   }

   private int _checkedFormatIndex = 0;
   private int _checkedParticipantsCountIndex = 0;
   private int _checkedParticipantsCount = 2;
}
